<?php

namespace app\teacher\model;

use app\common\model\RemindCourse as RemindCourseModel;
use think\Model;

class RemindCourse extends RemindCourseModel
{
    public function user()
    {
        return $this->hasOne(User::class, 'user_id', 'user_id')->field('user_id, nickname, avatar, mobile');
    }

    public function course()
    {
        return $this->hasOne(Course::class, 'course_id', 'course_id');
    }

    /**
     * 最新消息
     */
    public static function getByTeacherId($teacherId)
    {
        return self::where('teacher_id', $teacherId)->order('createtime desc')->find();
    }

    /**
     * 是否有未读
     */
    public static function getReadingByTeacherId($teacherId)
    {
        return self::where('teacher_id', $teacherId)->where('teacher_reading', 0)->count();
    }

    /**
     * 教师课程提醒
     */
    public static function getAllByTeacherId($teacherId, $size = 20)
    {
        $course = self::with(['user', 'courseOrder', 'course'])->where('teacher_id', $teacherId)->order('createtime desc')->paginate($size);
        foreach ($course->items() as $val) {
            $index = $val['course'] ? $val['course']['index'] : 0;
            $total = $val['course_order'] ? $val['course_order']['num'] : 0;
            $val['surplus'] = $total - $index;
        }
        return $course;
    }
}
